import axios from 'axios'
import { ElMessage } from 'element-plus'
import { ElLoading } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'
import 'element-plus/theme-chalk/el-loading.css'
import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
import type { WJAxiosRequestConfig } from './types'
import responseError from '../config/response-error'
class WJRequest {
  instance: AxiosInstance
  loading?: any
  isShowLoading: boolean
  constructor(config: WJAxiosRequestConfig) {
    this.instance = axios.create(config)
    this.isShowLoading = config.isShowLoading ?? true
    this.instance.interceptors.request.use(
      (config) => {
        if (this.isShowLoading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '正在请求数据',
            background: 'rgba(0,0,0,0.5)'
          })
        }

        // 携带token的拦截
        const token = ''
        if (token) {
          config.headers.Authorization = `Bearer ${token}`
        }
        return config
      },
      (err) => {
        return err
      }
    )
    this.instance.interceptors.response.use(
      (res) => {
        this.loading?.close()
        return res.data
      },
      async (err) => {
        // 对响应错误进行处理
        const { response } = err
        if (response) {
          // 服务器返回的状态码
          await responseError(response)
        } else {
          ElMessage({
            message: `络错误或请求未发出`,
            grouping: true,
            type: 'error'
          })
          console.error('网络错误或请求未发出', err.message)
          return Promise.reject(err)
        }
      }
    )
    this.instance.interceptors.request.use(
      config.interceptors?.requestSuccessInterceptors,
      config.interceptors?.requestErrorInterceptors
    )
    this.instance.interceptors.response.use(
      config.interceptors?.responseSuccessInterceptors,
      config.interceptors?.requestErrorInterceptors
    )
  }
  request<T = any>(config: WJAxiosRequestConfig<T>) {
    if (config.interceptors?.requestSuccessInterceptors) {
      config = config.interceptors.requestSuccessInterceptors(
        config as InternalAxiosRequestConfig
      )
    }
    if (config.isShowLoading === false) {
      this.isShowLoading = false
    }
    return new Promise<T>((resolve, reject) => {
      this.instance
        .request<any, T>(config)
        .then((res) => {
          if (config.interceptors?.responseSuccessInterceptors) {
            res = config.interceptors.responseSuccessInterceptors(res)
          }
          resolve(res)
          this.isShowLoading = true
        })
        .catch((err) => {
          reject(err)
          this.isShowLoading = true
        })
    })
  }
  get<T = any>(config: WJAxiosRequestConfig<T>) {
    return this.request({ ...config, method: 'GET' })
  }
  post<T = any>(config: WJAxiosRequestConfig<T>) {
    return this.request({ ...config, method: 'POST' })
  }
  patch<T = any>(config: WJAxiosRequestConfig<T>) {
    return this.request({ ...config, method: 'PATCH' })
  }
  delete<T = any>(config: WJAxiosRequestConfig<T>) {
    return this.request({ ...config, method: 'DELETE' })
  }
}
export default WJRequest
